﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LCUV;
using System.Data;
using Telerik.Web.UI;
using System.Text;

namespace sWarranty.WEB
{
    public partial class SchemeBalance : System.Web.UI.Page
    {
        public string _currentUserEmpNo = "";
        public string _currentUserAuthority = "";
        public string _currentUserRegion = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            _currentUserEmpNo = GetUserLoginEmpNo();
            Session["UrltoFw"] = "SchemeBalance.aspx";

            if (dropbillShop.SelectedValue == "bill")
            {
                RadGrid1.Visible = true;
                RadGrid2.Visible = false;
            }
            if (dropbillShop.SelectedValue == "shop")
            {
                RadGrid1.Visible = false;
                RadGrid2.Visible = true;
            }
            if (!IsPostBack)
            {
                DBAction.SetSelected(dropYear, DateTime.Now.Year.ToString());
                int monthTOShow = Convert.ToInt32(DateTime.Now.Month); //hom nay thu dateTOShow, thi chi hien den cot D dateTOShow
                if (monthTOShow >= 10)
                {
                    DBAction.SetSelected(dropMonth, DateTime.Now.Month.ToString());
                    DBAction.SetSelected(dropMonth2, DateTime.Now.Month.ToString());
                }
                else
                {
                    DBAction.SetSelected(dropMonth, "0" + DateTime.Now.Month.ToString());
                    DBAction.SetSelected(dropMonth2, "0" + DateTime.Now.Month.ToString());
                }
            }
        }
        protected string GetUserLoginEmpNo()
        {
            string t = "";
            try
            {
                if (Session["UserLoginInfo"] != null)
                {
                    Entities db = new Entities();
                    var u = (TB_USER[])Session["UserLoginInfo"];
                    int _UID = Convert.ToInt32(u.Single().ID);
                    var au = from ft in db.TB_USER_LEVEL
                             where ft.USER_ID == _UID
                             select ft;
                    int _le = Convert.ToInt32(au.Single().LEVEL_ID);
                    var le = from xx in db.TB_LEVEL
                             where xx.ID == _le
                             select xx;
                    _currentUserAuthority = le.Single().REMARK;
                    _currentUserRegion = u.Single().REGION_ID;
                    if (_currentUserRegion != "00")
                    {
                        DBAction.SetSelected(dropRegion, (u.Single().REGION_ID));
                        dropRegion.Enabled = false;
                    }
                    return u.Single().EMPNO;
                }
            }
            catch (Exception)
            {
                t = "";
            }
            return t;
        }
        protected void buildtable(string _month, string _month2, string _bill, string _shopName, string _region)
        {
            DataTable dt = new DataTable();
            if (dropbillShop.SelectedValue == "bill")
            {
                dt = DBAction.runStore("PK_SOM_MST.PR_SO_INCT_BAL_BILL_INQ", "uiroi", "p_ms_yyyymm", _month, "p_me_yyyymm", _month2,
                    "p_billto_name", _bill, "p_region_id", _region, "p_user_region", _currentUserRegion,
                    null, null, null, null, null, null, null, null
                    , null, null, null, null, null, null, null, null, null, null, null, null);
            }
            if (dropbillShop.SelectedValue == "shop")
            {

                dt = DBAction.runStore("PK_SOM_MST.PR_SO_INCT_BAL_SHOP_INQ", "uiroi", "p_ms_yyyymm", _month, "p_me_yyyymm", _month2,
                    "p_shop_name", _shopName, "p_region_id", _region, "p_user_region", _currentUserRegion, null,
                    null, null, null, null, null, null, null
                    , null, null, null, null, null, null, null, null, null, null, null, null);
            }
            try
            {
                if (dt.Rows.Count > 0 && !string.IsNullOrEmpty(dt.Rows[0]["Error"].ToString()))
                {
                    MessageBox.Show(dt.Rows[0]["Error"].ToString());
                    return;
                }
            }
            catch (Exception)
            {
                //Column 'Error' does not belong to table <-- ko co' loi j va co' data , thi ko lam j
            }
            RadGrid1.DataSource = dt;
            RadGrid1.DataBind();
            RadGrid2.DataSource = dt;
            RadGrid2.DataBind();

            ScriptManager.RegisterStartupScript(this, this.GetType(), "skldfjsldf", "reSelect()", true);

        }
        protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            //RadGrid1.DataSource = BuildTable(null, null, null, null, null);
            //RadGrid1.DataBind();
            //Page.ClientScript.RegisterStartupScript(this.GetType(), "click", "var btn=document.getElementById('btnSearch');btn.click();", true);
            string year = dropYear.SelectedValue;
            string month = dropMonth.SelectedValue;
            string month2 = dropMonth2.SelectedValue;
            string region = "%";
            string shopName = null;
            string bill = null;

            if (dropRegion.SelectedValue != "00")
                region = dropRegion.SelectedValue;
            if (!string.IsNullOrEmpty(txtShopName.Text.Trim()))
                shopName = txtShopName.Text.Trim();
            if (!string.IsNullOrEmpty(txtBill.Text.Trim()))
                bill = txtBill.Text.Trim();

            DataTable dt = new DataTable();
            if (dropbillShop.SelectedValue == "bill")
            {
                dt = DBAction.runStore("PK_SOM_MST.PR_SO_INCT_BAL_BILL_INQ", "uiroi", "p_ms_yyyymm",
                  year + month, "p_me_yyyymm", year + month2, "p_billto_name", bill, "p_region_id", region, "p_user_region",
                    _currentUserRegion, null, null, null, null, null, null, null, null
                    , null, null, null, null, null, null, null, null, null, null, null, null);
            }
            if (dropbillShop.SelectedValue == "shop")
            {
                dt = DBAction.runStore("PK_SOM_MST.PR_SO_INCT_BAL_SHOP_INQ", "uiroi", "p_yyyymm",
                  year + month, "p_me_yyyymm", year + month2, "p_shop_name", shopName, "p_region_id", region, "p_user_region",
                    _currentUserRegion, null, null, null, null, null, null, null, null
                    , null, null, null, null, null, null, null, null, null, null, null, null);
            }
            try
            {
                if (dt.Rows.Count > 0 && !string.IsNullOrEmpty(dt.Rows[0]["Error"].ToString()))
                {
                    MessageBox.Show(dt.Rows[0]["Error"].ToString());
                    return;
                }
            }
            catch (Exception)
            {
                //Column 'Error' does not belong to table <-- ko co' loi j va co' data , thi ko lam j
            }

            RadGrid1.DataSource = dt;
            RadGrid2.DataSource = dt;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "skldfjsldf", "reSelect()", true);
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string year = dropYear.SelectedValue;
            string month = dropMonth.SelectedValue;
            string month2 = dropMonth2.SelectedValue;
            string region = "%";
            string shopName = null;
            string bill = null;

            if (dropRegion.SelectedValue != "00")
                region = dropRegion.SelectedValue;
            if (!string.IsNullOrEmpty(txtShopName.Text.Trim()))
                shopName = txtShopName.Text.Trim();
            if (!string.IsNullOrEmpty(txtBill.Text.Trim()))
                bill = txtBill.Text.Trim();
            if (!string.IsNullOrEmpty(txtShopName.Text.Trim()))
                shopName = txtShopName.Text.Trim();

            MessageBox.Show(year + month);

            buildtable(year + month, year + month2, bill, shopName, region);

        }
        protected void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
        {
            if (e.Column is GridBoundColumn)
            {
                (e.Column as GridBoundColumn).DataFormatString = "<nobr>{0}</nobr>"; //auto width
                if ((e.Column as GridBoundColumn).HeaderText == "AMT" || (e.Column as GridBoundColumn).HeaderText == "QTY" || (e.Column as GridBoundColumn).HeaderText == "AMT")
                {
                    (e.Column as GridBoundColumn).HeaderStyle.HorizontalAlign = HorizontalAlign.Right;
                    (e.Column as GridBoundColumn).ItemStyle.HorizontalAlign = HorizontalAlign.Right;
                }
            }
        }

        //protected void dropbillShop_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    if (dropbillShop.SelectedValue == "bill")
        //    {
        //        txtBill.Visible = true;
        //        txtShopName.Visible = false;
        //    }
        //    if (dropbillShop.SelectedValue == "shop")
        //    {
        //        txtShopName.Visible = true;
        //        txtBill.Visible = false;
        //    }
        //    //RadGrid1.Rebind();
        //}
        protected void btnExcel_Click(object sender, EventArgs e)
        {
            RadGrid1.ExportSettings.IgnorePaging = true;
            RadGrid1.ExportSettings.OpenInNewWindow = true;
            RadGrid1.ExportSettings.FileName = "data";
            RadGrid1.MasterTableView.ExportToExcel();

        }
        protected void RadGrid1_GroupsChanging(object sender, GridGroupsChangingEventArgs e)
        {
            if (e.Action == GridGroupsChangingAction.Group)
                RadGrid1.MasterTableView.GetColumnSafe(e.Expression.GroupByFields[0].FieldName).Visible = false;
            else
                if (e.Action == GridGroupsChangingAction.Ungroup)
                    RadGrid1.MasterTableView.GetColumnSafe(e.Expression.GroupByFields[0].FieldName).Visible = true;
        }

        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (_currentUserRegion != "00")
            {
                GridColumn column = RadGrid1.MasterTableView.GetColumnSafe("REGION");
                if (column != null)
                {
                    column.Visible = false;
                }
            }
            if (e.Item is GridDataItem)
            {
                GridDataItem item = (GridDataItem)e.Item;
                item["INCT_AMT"].Text = String.Format("{0:#,0}", item["INCT_AMT"].Text);
            }

        }

        protected void RadGrid1_PreRender(object sender, EventArgs e)
        {
            //RadGrid1.MasterTableView.TableLayout = GridTableLayout.Fixed;
            //for (int i = 0; i < RadGrid1.MasterTableView.AutoGeneratedColumns.Length; i++)
            //    (RadGrid1.MasterTableView.AutoGeneratedColumns[i] as GridBoundColumn).DataFormatString = "<nobr>{0}</nobr>";
            //RadGrid1.Rebind();
        }
    }
}